/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman;

import com.google.common.base.CharMatcher;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.NumberFormat;

public final class Utils {
    private Utils() {
    }

    public static void overwriteWithTempFile(File tempFile, File realFile) throws IOException {
        Path source = tempFile.toPath();
        Path target = realFile.toPath();
        overwriteWithTempFile(source, target);
    }

    public static void overwriteWithTempFile(Path source, Path target) throws IOException {
        Files.deleteIfExists(target);
        Files.copy(source, target);
        Files.deleteIfExists(source);
    }

    public static boolean containsIgnoreCase(String one, String two) {
        if (one == null || two == null) {
            return false;
        }

        return one.toLowerCase().contains(two.toLowerCase());
    }

    @SuppressWarnings("StringConcatenationMissingWhitespace")
    public static String getNextKey(CharSequence keyName) {
        String keyBase = CharMatcher.DIGIT.removeFrom(keyName).trim();

        if (keyBase.endsWith("tt") || keyBase.endsWith("mn")) {
            keyBase = keyBase.substring(0, keyBase.length() - 2);
        }

        String numberString = CharMatcher.DIGIT.retainFrom(keyName);

        if (numberString.isEmpty()) {
            return "";
        }

        int value = Integer.parseInt(numberString);

        NumberFormat integerInstance = NumberFormat.getIntegerInstance();
        integerInstance.setMinimumIntegerDigits(numberString.length());
        integerInstance.setGroupingUsed(false);

        return keyBase + integerInstance.format(value + 1);
    }

    @SuppressWarnings("SimplifiableIfStatement")
    public static boolean equalsIgnoreCase(String s1, String s2) {
        if (s1 == null && s2 == null) {
            return true;
        }
        if (s1 == null || s2 == null) {
            return false;
        }
        return s1.toLowerCase().equals(s2.toLowerCase());
    }

    public static String getColorForCompleteness(double completeness) {
        String color;
        if (completeness < 0.5) {
            color = "#C65D57";
        } else if (completeness < 1) {
            color = "#E0D873";
        } else {
            color = "#D1E231";
        }
        return color;
    }
}
